<?php

class FTP
{
	function __construct($host, $username, $password, $port=21, $timeout=90)
   {
      $this->h = ftp_connect($host,$port,$timeout);
      if (!$this->h) {
         throw new Exception("ftp_connect($host,$port,$timeout)");
      }
      $this->login($username, $password);
   }

   function __destruct()
   {
      ftp_close($this->h);
   }

   public function chdir($path)
   {
      if (!ftp_chdir($this->h,$path)) {
         throw new Exception("ftp_chdir");
      }
   }
   
   public function getList($params='.')
   {
      $files = ftp_nlist($this->h,$params);
      if (!$files) {
         throw new Exception("ftp_nlist");
      }
      return $files;
   }

   /**
      @param $hfile  file handle
   */
   public function put($remotePath, $hfile)
   {
      if (!ftp_fput($this->h, $remotePath, $hfile, FTP_BINARY)) {
         throw new Exception("ftp_fput");
      }
   }  

   public function del($path)
   {
      if (!ftp_delete($this->h,$path)) {
         throw new Exception("ftp_delete");
      }
   }


   private function login($username, $password)
   {
      if (!ftp_login($this->h,$username,$password)) {
         throw new Exception("Couldn't connect as $username");
      }
   }

   private $h = false;
};

?>